package com.hxzy.yygh.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxzy.yygh.enums.RefundStatusEnum;
import com.hxzy.yygh.model.order.PaymentInfo;
import com.hxzy.yygh.model.order.RefundInfo;
import com.hxzy.yygh.order.mapper.RefundInfoMapper;
import com.hxzy.yygh.order.service.RefundInfoService;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundInfo> implements RefundInfoService {

    //保存退款记录
    @Override
    public RefundInfo saveRefundInfo(PaymentInfo paymentInfo) {
        //判断是否重复添加数据
        LambdaQueryWrapper<RefundInfo> lqw = new LambdaQueryWrapper<>();
        lqw.eq(RefundInfo::getOrderId, paymentInfo.getOrderId());
        lqw.eq(RefundInfo::getPaymentType, paymentInfo.getPaymentType());
        RefundInfo refundInfo = baseMapper.selectOne(lqw);
        if (refundInfo != null){
            return refundInfo;
        }
        //若没有此条数据，再进行添加
        refundInfo = new RefundInfo();
            //封装参数
            refundInfo.setCreateTime(new Date());
            refundInfo.setOrderId(paymentInfo.getOrderId());
            refundInfo.setPaymentType(paymentInfo.getPaymentType());
            refundInfo.setOutTradeNo(paymentInfo.getOutTradeNo());
            refundInfo.setRefundStatus(RefundStatusEnum.UNREFUND.getStatus());
            refundInfo.setSubject(paymentInfo.getSubject());
            refundInfo.setTotalAmount(paymentInfo.getTotalAmount());
            //添加数据
            baseMapper.insert(refundInfo);
        return refundInfo;
    }
}
